export default (() => {
  const getDateObj = ms => {
    const now = ms ? new Date(ms) : new Date()
    const y = now.getFullYear()
    let m = now.getMonth() + 1
    m = m > 9 ? m : '0' + m
    let d = now.getDate()
    d = d > 9 ? d : '0' + d
    return { y, m, d }
  }
  // 获取当前日期
  const getCurrentDate = ms => {
    const dateObj = getDateObj(ms)
    return '' + dateObj.y + '-' + dateObj.m + '-' + dateObj.d
  }
  //获取当前 前后N天的时间
  const getDateStr = (AddDayCount = 0) => {
    let dd = new Date()
    dd.setDate(dd.getDate() + AddDayCount)
    let y = dd.getFullYear()
    let m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1) //获取当前月份的日期，不足10补0
    let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() //获取当前几号，不足10补0
    return y + '-' + m + '-' + d
  }
  //获取某个日期 前后N天的时间
  const getDateDay2 = (time, AddDayCount) => {
    if (time) {
      let dd = new Date(time)
      dd.setDate(dd.getDate() + AddDayCount)
      let y = dd.getFullYear()
      let m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1) //获取当前月份的日期，不足10补0
      let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() //获取当前几号，不足10补0
      return y + '-' + m + '-' + d
    } else {
      return '--'
    }
  }
  //获取当前年份
  const getCurrentYear = ms => {
    const dateObj = getDateObj(ms)
    return '' + dateObj.y
  }
  // 获取当前月份
  const getCurrentMonth = ms => {
    const dateObj = getDateObj(ms)
    return '' + dateObj.y + '-' + dateObj.m
  }
  // 获取上月月份
  const getPreviousMonth = ms => {
    const dateObj = getDateObj(ms)
    const b = dateObj.m - 1 > 0
    let y = b ? dateObj.y : dateObj.y - 1
    let m = b ? dateObj.m - 1 : 12
    m = m > 9 ? m : '0' + m
    return '' + y + '-' + m
  }
  // 获取下月月份
  const getNextMonth = ms => {
    const dateObj = getDateObj(ms)
    const b = dateObj.m - (-1) != 13;
    let y = b ? dateObj.y : dateObj.y + 1
    let m = b ? dateObj.m - (-1) : 1
    m = m > 9 ? m : '0' + m
    return '' + y + '-' + m
  }
  // 格式化时间 ==> yyyy-mm-dd
  const formatDate = (value, str) => {
    if (value) {
      let date_arr = value.split(str)
      let m = date_arr[1].length == 1 && date_arr[1] < 10 ? '0' + date_arr[1] : date_arr[1]
      let d = date_arr[2].length == 1 && date_arr[2] < 10 ? '0' + date_arr[2] : date_arr[2]
      return date_arr[0] + '-' + m + '-' + d
    } else {
      return '--'
    }
  }
  const getK = value => {
    if (value) {
      let date = new Date(value)
      return date
    } else {
      let v = new Date().getTime()
      return v
    }
  }
  //转为时间戳
  const formatTime = value => {
    if (value) {
      let date = new Date(value)
      return date.getTime()
    }
  }
  const formatDate2 = (time, format = 'YY-MM-DD hh:mm:ss') => {
    if (time) {
      let date = time ? new Date(time) : new Date()
      let year = date.getFullYear(),
        month = date.getMonth() + 1, //月份是从0开始的
        day = date.getDate(),
        hour = date.getHours(),
        min = date.getMinutes(),
        sec = date.getSeconds()
      let preArr = Array.apply(null, Array(10)).map(function (elem, index) {
        return '0' + index
      }) //开个长度为10的数组 格式为 00 01 02 03

      let newTime = format
        .replace(/YY/g, year)
        .replace(/MM/g, preArr[month] || month)
        .replace(/DD/g, preArr[day] || day)
        .replace(/hh/g, preArr[hour] || hour)
        .replace(/mm/g, preArr[min] || min)
        .replace(/ss/g, preArr[sec] || sec)

      return newTime
    } else {
      return '--'
    }
  }

  const getLastDay = dateStr => {
    dateStr = dateStr || getCurrentMonth()
    let nextMonthMS = new Date(getNextMonth(new Date(dateStr).getTime())).getTime()
    return getCurrentDate(new Date(nextMonthMS - 1000 * 60 * 60 * 24).getTime())
  }

  // 对比时间
  const compareTime = (date1, date2) => {
    let d1_date1 = new Date(date1),
      d1_date2 = new Date(date2);
    let time_span1 = d1_date1.getTime(),
      time_span2 = d1_date2.getTime();
    return time_span1 - time_span2;
  }
  //格式yy-mm month1小的  month2大的
  const intervalMonth = (month1, month2) => {
    let yearMonth, number;
    let year1 = Number(getCurrentYear(month1));
    let year2 = Number(getCurrentYear(month2));
    if (year1 != year2) {
      yearMonth = (year2 - year1) * 12;
    } else {
      yearMonth = 0;
    }
    let min = Number(month1.split('-')[1]);
    let max = Number(month2.split('-')[1]);
    if (min < max) {
      number = max - min + yearMonth + 1
    } else {
      number = yearMonth - (min - max) + 1
    }
    return Number(number);
  }
  //获取季度的时间 val 例如 yyyy,1 最终结果  yyyy-mm-dd,yyyy-mm-dd
  const qurterDay = (val) => {
    let value;
    let num = val.split(',')[1]
    if (num == 1) {
      value = val.split(',')[0] + '-01-01,' + val.split(',')[0] + '-03-31'
    } else if (num == 2) {
      value = val.split(',')[0] + '-04-30,' + val.split(',')[0] + '-06-30'
    } else if (num == 3) {
      value = val.split(',')[0] + '-07-31,' + val.split(',')[0] + '-09-30'
    } else if (num == 4) {
      value = val.split(',')[0] + '-10-30,' + val.split(',')[0] + '-12-31'
    }
    return [value, num];
  }
  //获取某月最后一天
  const getMonthLastDay = (dateStr) => {
    let dateObj = getNextMonth(dateStr)
    let date = getDateDay2(dateObj + '-01', -1)
    return date
  }
  /*获取当前日期*/
  const getWeekdayTime = (value) => {
    let d = new Date(value)
    let week = d.getDay()
    let weekday = ''
    if (week == 0)
      weekday = '星期日'
    else if (week == 1)
      weekday = '星期一'
    else if (week == 2)
      weekday = '星期二'
    else if (week == 3)
      weekday = '星期三'
    else if (week == 4)
      weekday = '星期四'
    else if (week == 5)
      weekday = '星期五'
    else if (week == 6)
      weekday = '星期六'
    return weekday
  }
  //计算当前时间到今天结束时还剩多少 毫秒
  const getRestOfDayTime = () => {
    let x = new Date();
    x.setHours(0, 0, 0, 0);
    let y = new Date();
    return 24 * 3600 * 1000 - (y.getTime() - x.getTime())
  }
  /* 用于月日这种需要加前置0的方法
  *  @param num  需要加0的参数 string | number
  *  @return 结果 始终保证结果为字符串  string
  */
  const paddingZero = (num) => {
    return num < 10 ? "0" + num : String(num)
  }
  return { paddingZero, getDateObj, getCurrentDate, getDateStr, getDateDay2, getCurrentYear, getCurrentMonth, getPreviousMonth, getNextMonth, formatDate, formatDate2, formatTime, getK, compareTime, getLastDay, intervalMonth, getMonthLastDay, qurterDay, getWeekdayTime, getRestOfDayTime }
})()
